package com.itcam;

import java.util.Scanner;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class Demo04 {

    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(System.in);

            System.out.println("请输入数字A：");
            double numberA = Double.parseDouble(scanner.nextLine());
            System.out.println("请选择运算符（+、-、*、/）");
            String operation = scanner.nextLine();
            System.out.println("请输入数字B：");
            double numberB = Double.parseDouble(scanner.nextLine());
            Operation02 o = OperationFactory.createOperation(operation);
            double result = o.getResult(numberA, numberB);


            System.out.println("结果是：" + result);
        } catch (NumberFormatException e) {
            System.out.println("输入有误" + e.toString());
        }
    }

}

class Operation02 {
    public double getResult(double numberA, double numberB) {
        return 0d;
    }
}

class Add extends Operation02 {
    public double getResult(double numberA, double numberB) {
        return numberA + numberB;
    }
}

class Sub extends Operation02 {
    public double getResult(double numberA, double numberB) {
        return numberA - numberB;
    }
}

class Mul extends Operation02 {
    public double getResult(double numberA, double numberB) {
        return numberA * numberB;
    }
}

class Div extends Operation02 {
    public double getResult(double numberA, double numberB) {
        if (numberB == 0) {
            System.out.println("被除数不能为0");
            throw new ArithmeticException();
        }
        return numberA / numberB;
    }
}

class Pow extends Operation02 {
    public double getResult(double numberA, double numberB) {
        return Math.pow(numberA, numberB);
    }
}

// 简单运算工厂类
class OperationFactory {

    public static Operation02 createOperation(String operation) {
        Operation02 o = null;
        switch (operation) {
            case "+":
                o = new Add();
                break;
            case "-":
                o = new Sub();
                break;
            case "*":
                o = new Mul();
                break;
            case "/":
                o = new Div();
                break;
            case "pow":
                o = new Pow();
                break;
        }
        return o;
    }
}
